package com.zhou.controller;

import com.zhou.entity.Input;
import com.zhou.service.InputService;
import com.zhou.utis.result.Result;
import com.zhou.vo.inputVo.InputVo;
import com.zhou.vo.inputVo.UpdateInputVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;

/**
 * @author zhouyuyan
 */
@RestController
@RequestMapping("input")
public class InputController {
    @Autowired
    private InputService inputService;

    /**
     * 入库添加
     */
    @PostMapping("add")
    public Result save(@RequestBody InputVo vo) {
        try {
            Boolean save = inputService.save(vo);
            return Result.success(save);
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 入库修改
     */
    @PutMapping("update")
    public Result update(@RequestBody UpdateInputVo vo) {
        try {
            Boolean update = inputService.update(vo);
            return Result.success(update);
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 入库删除
     */
    @DeleteMapping("delete")
    public Result delete(@RequestParam String id) {
        try {
            Boolean delete = inputService.delete(id);
            return Result.success(delete);
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 根据id查询
     */
    @GetMapping("getId")
    public Result getById(@RequestParam("id") String id) {
        try {
            Input byId = inputService.getById(id);
            return Result.success(byId);
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 入库分页查询
     */
    @GetMapping("page")
    public Result getAll(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize) {
        try {
            List<Input> all = inputService.getAll(page, pageSize);
            return Result.success(all);
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        }
    }
}
